package com.jpa.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jpa.Interceptor.adminInterceptor;
import com.jpa.bean.PageBeanTwo;
import com.jpa.entity.Affairs;
import com.jpa.entity.Useradmin;
import com.jpa.service.AffairsService;
import com.jpa.service.RedisService;

@RestController
public class AffairsController extends BaseController {
	@Reference(version = "1.0.0")
	private AffairsService as;
	private final static Logger logger = LoggerFactory.getLogger(AffairsController.class);
	@Reference(version = "1.0.0")
	private RedisService rs;
	// 迭代查询所有用户信息
	@GetMapping(path = "admin/findAffairsAll")
	public Object findAffairsAll(Integer start, Integer size) {
		if (start != null && size != null) {
			PageBeanTwo pageBeanTwo = new PageBeanTwo();
			pageBeanTwo.setPage(start);
			pageBeanTwo.setSize(size);
			pageBeanTwo.setDesc("id");
			return renderSuccess(as.findAffairsAll(pageBeanTwo));
		} else {
			return renderError();
		}

	}

	@GetMapping(path = "admin/findAffairsByTitle")
	public Object findAffairsByTitle(String title,Integer start,Integer size) {
		if (start != null && size != null&&title!=null) {
			PageBeanTwo pageBeanTwo = new PageBeanTwo();
			pageBeanTwo.setPage(start);
			pageBeanTwo.setSize(size);
			pageBeanTwo.setDesc("id");
			return renderSuccess(as.findAffairsByTitleLike("%" + title + "%", pageBeanTwo));
		} else {
			return renderError();
		}

	}

	@GetMapping(path = "admin/saveAffairs")
	public  Object saveAffairs(Affairs affairs,HttpServletRequest arg0) {
		if (affairs != null && affairs.getTitle() != null) {
			try {
				affairs.setUseradmin((Useradmin) arg0.getAttribute("useradmin"));
				if (as.save(affairs) == true)
					return renderSuccess("成功");
				else
					return renderSuccess("失败");
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(e.toString());
				return renderSuccess("失败");
			}
		} else {
			return renderError("失败");
		}

	}

	@GetMapping(path = "admin/findAffairsById")
	public Object findAffairsById(Long id) {
		if (id != null) {
			return renderSuccess(as.findAffairsById(id));
		} else {
			return renderError();
		}

	}

}
